package com.ts.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.core.entity.Vo.BloggerVo;
import com.ts.core.entity.LxbBlogger;
import com.ts.core.service.LxbBloggerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-03-19
 */

@Api(tags = "博主管理")
@RestController
@RequestMapping("/admin/core/lxbBlog")
public class LxbBloggerController {
    @Resource
    private LxbBloggerService lxbBloggerService;


    @GetMapping("/insertOne")
    public String insertIntoTable()
    {
        System.out.println("==============");
        return "helloword";
    }

    @ApiOperation("分页查询博主列表")
    @GetMapping("/list/{page}/{limit}")
    public Map<String,Object> listPage(
            @PathVariable Long page,
            @PathVariable Long limit,

            @ApiParam(required = false)
            @RequestParam String keyword
    ){
        Page<LxbBlogger> page1 = new Page<>(page, limit); //构造分页对象
        IPage<LxbBlogger> iPage =lxbBloggerService.getlistPage(page1,keyword);

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("messsage","成功");

        HashMap<String, Object> data = new HashMap<>();
        data.put("pageModel",iPage);
        map.put("data",data);


        return map;


    }

    @ApiOperation("根据id更改博主状态")
    @GetMapping("/alterSort/{id}")
    public Map<String,Object> AlterSort(
            @PathVariable String id
    ){

        lxbBloggerService.SetState(id);

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("messsage","成功");
        return map;

    }


    //
    @ApiOperation("逻辑删除")
    @GetMapping("/deleteById/{id}")
    public Map<String,Object> deleteById(
            @PathVariable String id

    ){

        lxbBloggerService.removeById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","删除成功");
        return map;

    }

    @ApiOperation("添加新的博主")
    @PostMapping("/insertBlogger")
    public Map<String,Object> InsertBlogger(
            @RequestBody BloggerVo bloggerVo
            ){
        Boolean b= lxbBloggerService.insertBloggerToTable(bloggerVo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        if (b) {

            map.put("message","添加成功");


        }
        else
        {
            map.put("message","添加失败");
        }
        return map;



    }

    @ApiOperation("根据id查询博主信息")
    @GetMapping("/getInfoById/{id}")
    public Map<String,Object> getInfoFromTable(
            @PathVariable String id
    ){
        LxbBlogger lxbBlogger = lxbBloggerService.getById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("messsage","成功");

        HashMap<String, Object> data = new HashMap<>();
        data.put("blogger",lxbBlogger);
        map.put("data",data);


        return map;

    }



    @PostMapping("/updateBlogger")
    public Map<String,Object> updateBlogger(@RequestBody LxbBlogger lxbBlogger

    ){
        boolean b = lxbBloggerService.updateById(lxbBlogger);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        if (b) {
            map.put("message","修改成功");
        }
        else{
            map.put("message","修改失败");

        }
        return map;



    }











}

